package com.IClass.IActivity;

import java.util.HashMap;
import java.util.Map.Entry;

import org.json.JSONException;
import org.json.JSONObject;

import com.IClass.IClassApi;
import com.IClass.ImageUtil;
import com.IClass.NetUploadPictureListen;
import com.IClass.App.IClass;
import com.IClass.App.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class ILookAskActivity extends Activity{

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(data != null)
		{
			Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");

			selectimage.setImageBitmap(cameraBitmap);

			String strImgPath = Environment.getExternalStorageDirectory()
					.toString();
			strImgPath += "/ioclass/imagescach/bbstextselectimage.png";

			ImageUtil.saveBitmap(cameraBitmap, strImgPath);
		}

		  
		  
	}
	public ImageView selectimage;
	public EditText title;
	public EditText text;
	public Button btnsend;
	public Button btnreturn;
	public Context context;
	
	public ProgressDialog progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// FIXME Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		this.setContentView(R.layout.ilook_sendtext_layout);
		
		selectimage = ((ImageView)findViewById(R.id.imageView1));
		title = ((EditText)findViewById(R.id.editText1));
		text = ((EditText)findViewById(R.id.editText2));
		
		btnreturn = ((Button)findViewById(R.id.button1));
		btnsend = ((Button)findViewById(R.id.button2));
		
		context = this;
		selectimage.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
			    intent.addCategory(Intent.CATEGORY_OPENABLE);
			    intent.setType("image/*");
			    
			    intent.putExtra("crop", "true");
			    intent.putExtra("aspectX", 1);
			    intent.putExtra("aspectY", 1);
			    intent.putExtra("outputX", 80);
			    intent.putExtra("outputY", 80);
			    intent.putExtra("return-data", true);

			    startActivityForResult(intent, 0);

			}
			
			
		});
		btnsend.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				SharedPreferences  share = context.getSharedPreferences("IClass", MODE_PRIVATE);  
		        String userid = share.getString("userid", "0");
		        
		        if(userid == "0")
		        {
					Intent mainIntent = new Intent(ILookAskActivity.this,IRegisterActivity.class); 
		            startActivity(mainIntent);    //启动Main界面
		            return;
		        }
		        //setProgressBarIndeterminateVisibility(true);
		        progressDialog = ProgressDialog.show(ILookAskActivity.this, "请稍等...","发布帖子中...", true);
		        
		        
		        
				// TODO Auto-generated method stub
		        String strImgPath = Environment.getExternalStorageDirectory().toString();
				strImgPath += "/ioclass/imagescach/bbstextselectimage.png";
				  
				JSONObject jsonobj = new JSONObject();
				
				try {
					jsonobj.put("title", title.getText().toString());
					jsonobj.put("text", text.getText().toString());
					jsonobj.put("type", "blog");
					//IClass.classapi.SendBBSText(userid, strImgPath, jsonobj.toString(), new NetUploadPictureListen(){
					new LoaderThread(userid,strImgPath,jsonobj,handler).start();

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
			
		});
		btnreturn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ILookAskActivity.this.finish();
			}
			
			
		});
		
	}
	public Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// FIXME Auto-generated method stub
			super.handleMessage(msg);
			progressDialog.dismiss();
			
		}
	};
	@SuppressWarnings("unused")
	private class LoaderThread extends Thread {

    	private String userid;
    	private String strImgPath;
    	private JSONObject jsonobj;
    	private Handler mhandle;
    	public LoaderThread(String userid, String strImgPath, JSONObject jsonobj, Handler mhandle)
    	{
    		this.userid = userid;
    		this.strImgPath = strImgPath;
    		this.jsonobj = jsonobj;
    		this.mhandle = mhandle;
    		
		}
		@Override
		public void run() {
			// FIXME Auto-generated method stub
			super.run();
			IClass.classapi.SendBBSText(userid, strImgPath, jsonobj.toString(), new NetUploadPictureListen(){

				@Override
				public void OnResult(String resutl) {
					// FIXME Auto-generated method stub
					Message msg = new Message();
					msg.what = 1111;
					mhandle.sendMessage(msg);
				}
				
				
			});

		}
    	
    }
}
